﻿using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;
using NetduinoUtilities;
using NetduinoUtilities.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.IO;
using SecretLabs.NETMF.IO;
using System.IO.Ports;

namespace NetduinoUtilities.Hardware.Demo
{
    /// <summary>
    /// Demo program used to test some of the class libraries that I create
    /// </summary>
    /// <remarks></remarks>
    public class Program
    {
        #region Public Static Methods

        /// <summary>
        /// Mains this instance.
        /// </summary>
        /// <remarks></remarks>
        public static void Main()
        {
            // Defines the first SPI slave device with pin 10 as SS
            //SPI.Configuration Device1 = new SPI.Configuration(
            //    Pins.GPIO_PIN_D7, // SS-pin
            //    false, // SS-pin active state
            //    0, // The setup time for the SS port
            //    0, // The hold time for the SS port
            //    true, // The idle state of the clock
            //    false, // The sampling clock edge
            //    1000, // The SPI clock rate in KHz
            //    SPI_Devices.SPI1 // The used SPI bus (refers to a MOSI MISO and SCLK pinset)
            //    );

            //SPI SPIBus = new SPI(Device1);
            //SPIBus.Config = Device1;
            // default CS pin on Netduino Mini is Pin 13:    

            StorageDevice.MountSD("SD1", SPI_Devices.SPI1, Pins.GPIO_PIN_D13);
            string[] directories = System.IO.Directory.GetDirectories(@"\");

            Debug.Print("directory count: " + directories.Length.ToString());
            for (int i = 0; i < directories.Length; i++)
            {
                Debug.Print("directory: " + directories[i]);
            }

            string[] files = System.IO.Directory.GetFiles(@"\SD1");
            Debug.Print("file count: " + files.Length.ToString());

            for (int i = 0; i < files.Length; i++)
            {
                Debug.Print("filename: " + files[i]);
                FileStream fs = new FileStream(files[i], FileMode.Open, FileAccess.Read, FileShare.None, 512);
                StreamReader sr = new StreamReader(fs);
                Debug.Print("contents: " + sr.ReadToEnd());
            }

            OutputPort onboardLed = new OutputPort(Pins.ONBOARD_LED, false);
            onboardLed.Write(true);
            Thread.Sleep(100);
            onboardLed.Write(false);
        }

        #endregion
    }
}